/**
 * Copyright (c) 2006-2016 Huize Ltd. All Rights Reserved. 
 *  
 * This code is the confidential and proprietary information of   
 * Hzins. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Huize,http://www.huize.com.
 *  
 */
package com.agan.concurrent;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 有10个运动员赛跑，开跑之前，裁判需要等待10个运动员都准备好才能发令，并且10个运动员准备好之后也都需要等待裁判发令 才能开跑。
 */
public class CountDownLatchTest2 {

    public static void main(String[] args) throws InterruptedException {
	// 用于判断发令之前运动员是否已经完全进入准备状态，需要等待5个运动员，所以参数为10
	final CountDownLatch runner = new CountDownLatch(10);
	// 用于判断裁判是否已经发令，只需要等待一个裁判，所以参数为1
	final CountDownLatch referee = new CountDownLatch(1);

	ExecutorService executor = Executors.newCachedThreadPool();

	for (int i = 1; i <= 10; i++) {
	    final int offset = i;
	    executor.execute(new Runnable() {

		@Override
		public void run() {
		    try {
			TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
			System.out.println("运动员【" + offset + "】准备完毕" + Thread.currentThread().getName());
			runner.countDown();
			referee.await();
			System.out.println("运动员【" + offset + "】开跑。。。" + Thread.currentThread().getName());
		    } catch (InterruptedException e) {
			e.printStackTrace();
		    }
		}

	    });
	}

	runner.await();
	referee.countDown();
	System.out.println("裁判：所有运动员准备完毕，开始...");

	executor.shutdown();
    }
}
